package cn.edu.scau.cmi.liuxintong;

import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;

import cn.edu.scau.cmi.liuxintong.consistentComposite.ConsistentComponent;
import cn.edu.scau.cmi.liuxintong.consistentComposite.ConsistentComposite;
import cn.edu.scau.cmi.liuxintong.consistentComposite.ConsistetPerson;
import cn.edu.scau.cmi.liuxintong.safeComposite.SafeComponent;
import cn.edu.scau.cmi.liuxintong.safeComposite.SafeComposite;
import cn.edu.scau.cmi.liuxintong.safeComposite.SafePerson;
import javafx.application.Application;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.effect.DropShadow;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class SafeCompositeClient extends Application{
	TextArea textArea = new TextArea();
	SafePerson[] person=new SafePerson[100];
	SafeComposite[] team=new SafeComposite[100];
	int personNumber=0;
	int teamNumber=0;
	String name;
	@Override
	public void start(Stage primaryStage) throws Exception {
		// TODO Auto-generated method stub
		AnchorPane anchorPane = new AnchorPane();
		DropShadow dropshadow = new DropShadow();
		dropshadow.setColor(Color.PALEVIOLETRED);
		dropshadow.setOffsetX(5.0);
		dropshadow.setOffsetY(5.0);
		dropshadow.setRadius(10.0);
		Text experimentName = new Text("安全组织实验");//ok
		experimentName.setEffect(dropshadow);
		experimentName.setFont(Font.font(" Verdana",FontPosture.ITALIC,40));
	   String[] greetings=new String[] {"请选择实现功能","1.创建的个人的名字","2.组织的名字",
	    "3.输入组织的名字下的个人（输入格式例：组织名字   个人名字1   个人名字2）","4.输出组织下的人员名单","5.删除组织下的人员（输入格式例：组织名字   个人名字1   个人名字2）"};
	    ChoiceBox<String> choiceBox = new ChoiceBox((FXCollections.observableArrayList("请选择实现功能","1.创建的个人的名字","2.组织的名字",
	    "3.输入组织的名字下的个人（输入格式例：组织名字   个人名字1   个人名字2）","4.输出组织下的人员名单","5.删除组织下的人员（输入格式例：组织名字   个人名字1   个人名字2）")));
	    choiceBox.getSelectionModel().select(0);
		TextField textField = new TextField();
		textArea.setPrefHeight(330);
	    textArea.setPrefWidth(780);
	    anchorPane.getChildren().addAll(experimentName,textField,choiceBox,textArea);
		choiceBox.getSelectionModel().selectedIndexProperty().addListener(
	    		(ObservableValue<? extends Number> ov,
	    				Number old_val,Number new_val)->{
	    					switch(greetings[new_val.intValue()]) {
	    					case "请选择实现功能":
	    						textArea.appendText("请选择实现功能\n");
	    						break;
	    					case "1.创建的个人的名字":
	    						name=textField.getText();
	    						if(personNumber>=100) {
	    							textArea.appendText("个人对象已建立满");
	    						}
	    						else {
	    							person[personNumber]=new SafePerson();
	    							person[personNumber].setName(name);
	    							personNumber++;
	    							textArea.appendText("个人对象"+name+"已创建\n");
	    						}
	    						break;
	    					case "2.组织的名字":
	    						name=textField.getText();
	    						  if(teamNumber>=100) {
	    							  textArea.appendText("组织对象已建立满");
	    						    }
	    						    else {
	    						    	team[teamNumber]=new SafeComposite();
	    						    	team[teamNumber].setName(name);
	    						    	teamNumber++;
	    						    	textArea.appendText("组织对象"+name+"已创建\n"); 
	    						    }
	    						   
	    							break;
	    					case "3.输入组织的名字下的个人（输入格式例：组织名字   个人名字1   个人名字2）":
	    						name=textField.getText();
	    						String getname[]=name.split(" ");
	    						int theteam = 0;
	    						int [] thePerson=new int[getname.length];
	    						for(int j=0;j<=teamNumber;j++) {
	    							if(team[j].getName().equals(getname[0]))
	    							theteam=j;
	    							break;
	    							}
	    						for(int j=1;j<getname.length;j++) {      
	    			            for(int i=0;i<=personNumber;i++) {
	    			            if(person[i].getName().equals(getname[j])) {
	    			            thePerson[j-1]=i;
	    			            break;
	    			            }
	    			            }
	    			            }
	    						for(int j=0;j<thePerson.length;j++) {
	    			        		team[theteam].addChild(person[thePerson[j]]);
	    			        	}
	    						textArea.appendText("已插入\n"); 
	    			        	break;
	    					case "4.输出组织下的人员名单":
	    						name=textField.getText();
	    						int thisteam = 0;
	    						  for(int j=0;j<=team.length;j++) {
	    								if(team[j].getName().equals(name))
	    									thisteam=j;
	    								break;
	    						}
	    						  Set<SafeComponent> children = team[thisteam].getChildren();
	    						  Iterator<SafeComponent> teamChildrenIterator =  children .iterator();
	    						  while (teamChildrenIterator.hasNext()) {
	    								SafeComponent thisperson = teamChildrenIterator.next();
	    								textArea.appendText(thisperson.getName()+"\n");
	    					        }
	    						  break;
	    					case "5.删除组织下的人员（输入格式例：组织名字   个人名字1   个人名字2）":
	    						name=textField.getText();
	    						String getname1[]=name.split(" ");
	    						int theteam1 = 0;
	    						int [] thePerson1=new int[getname1.length];
	    						for(int j=0;j<=teamNumber;j++) {
	    							if(team[j].getName().equals(getname1[0]))
	    							theteam=j;
	    							break;
	    							}
	    						 for(int j=1;j<getname1.length;j++) {      
	    				          for(int i=0;i<=personNumber;i++) {
	    				            		if(person[i].getName().equals(getname1[j])) {
	    				            			thePerson1[j-1]=i;
	    				            			break;
	    				           }
	    				          }
	    				          }
	    						 for(int j=0;j<thePerson1.length;j++) {
	    			            		team[theteam1].removeChild(person[thePerson1[j]]);
	    			             }
	    						 textArea.appendText("已删除\n"); 
	    			            	break; 
	    					}
	    				});
	     AnchorPane.setLeftAnchor(experimentName, 275.0);
		 AnchorPane.setBottomAnchor(experimentName,400.0);//下
		 AnchorPane.setLeftAnchor(textField, 580.0);
		 AnchorPane.setBottomAnchor(textField,350.0);//下
		 AnchorPane.setLeftAnchor( choiceBox, 0.0);
		 AnchorPane.setBottomAnchor( choiceBox,350.0);//下
		 AnchorPane.setLeftAnchor(textArea, 10.0);
		 AnchorPane.setBottomAnchor(textArea,10.0);
		 primaryStage.setScene(new Scene(anchorPane, 800,450));
	     primaryStage.show();
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		 launch(args);
	}

}
